package cn.org.timespace.easyframe.dao.permission.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import cn.org.timespace.easyframe.action.permission.EfDictionaryModel;
import cn.org.timespace.easyframe.dao.EfBaseDAO;
import cn.org.timespace.easyframe.dao.permission.EfDictionaryDAO;
import cn.org.timespace.easyframe.model.permission.EfDictionary;
import cn.org.timespace.easyframe.model.permission.EfDictionaryId;
import cn.org.timespace.easyframe.util.DaoUtil;


/**
 * A data access object (DAO) providing persistence and search support for
 * EfDictionary entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see cn.org.timespace.hms.model.repast.EfDictionary
 * @author MyEclipse Persistence Tools
 */

public class EfDictionaryDAOImpl extends EfBaseDAO implements EfDictionaryDAO {
	
	public static final String TYPE = "type";
	public static final String ITEMNAME = "itemname";
	public static final String FILTER = "filter";
	public static final String EXESQL = "exesql";
	public static final String REMARK = "remark";

	public void save(EfDictionary transientInstance) {
		log.debug("saving EfDictionary instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re; 
		}
	}

	public void delete(EfDictionary persistentInstance) {
		log.debug("deleting EfDictionary instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public EfDictionary findById(EfDictionaryId id) {
		log.debug("getting EfDictionary instance with id: " + id);
		try {
			EfDictionary instance = (EfDictionary) getSession().get(
					"cn.org.timespace.easyframe.model.permission.EfDictionary", id);
			
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List<EfDictionary> findByCondition(EfDictionaryModel model) {
		log.debug("finding all EfDictionary instances");
		try {
			StringBuffer queryString = new StringBuffer("from EfDictionary a where 1=1 ");
			
			if(!StringUtils.isEmpty(model.getRemark())){
				queryString.append(" and a.remark like '%"+model.getRemark().trim()+"%' ");
			}
			
			if(!StringUtils.isEmpty(model.getGroupName())){
				queryString.append(" and a.id.groupname = '"+model.getGroupName()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getType())){
				queryString.append(" and a.type = '"+model.getType()+"' ");
			}
			
			queryString.append(" order by a.type desc,a.id.groupname,a.id.itemvalue ");
			
			return getHibernateTemplate().find(queryString.toString());
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List<EfDictionary> findByGroupName(String groupName) {
		log.debug("finding all EfDictionary instances");
		try {
			StringBuffer queryString = new StringBuffer("from EfDictionary a where 1=1 ");
			
			if(!StringUtils.isEmpty(groupName)){
				queryString.append(" and a.id.groupname = '"+groupName+"' ");
			}
			
			queryString.append(" order by a.type desc,a.id.groupname,a.id.itemvalue ");
			
			return getHibernateTemplate().find(queryString.toString());
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List findByExample(EfDictionary instance) {
		log.debug("finding EfDictionary instance by example");
		try {
			List results = getSession().createCriteria(
					"cn.org.timespace.easyframe.model.permission.EfDictionary").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding EfDictionary instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from EfDictionary as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List findByItemname(Object itemname) {
		return findByProperty(ITEMNAME, itemname);
	}

	public List findByFilter(Object filter) {
		return findByProperty(FILTER, filter);
	}

	public List findByExesql(Object exesql) {
		return findByProperty(EXESQL, exesql);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findAll() {
		log.debug("finding all EfDictionary instances");
		try {
			String queryString = "from EfDictionary";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public EfDictionary merge(EfDictionary detachedInstance) {
		log.debug("merging EfDictionary instance");
		try {
			EfDictionary result = (EfDictionary) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(EfDictionary instance) {
		log.debug("attaching dirty EfDictionary instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(EfDictionary instance) {
		log.debug("attaching clean EfDictionary instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void update(EfDictionary instance) {
		getHibernateTemplate().update(instance);
	}

	
	/**
	 * 添加table类型的数据字典时，自动生成itemValue(现有最大的+1)
	 */
	public void saveWithAutoGeneratorItemValue(EfDictionary efDictionary, String groupName) {
		
		log.debug("saveWithAutoGeneratorItemValue(EfDictionary efDictionary, String groupName)");
		try {
			
			String hql = null;
			Session session = getSession();
			Query q = null;
			List queryResultList = null;
			
			
			int itemValue = DaoUtil.getMaxItemValueInGroup(session, groupName)+1;
			
			//保存
			EfDictionaryId efDictionaryId = new EfDictionaryId();
			efDictionaryId.setGroupname(groupName);
			efDictionaryId.setItemvalue(itemValue+"");
			
			efDictionary.setId(efDictionaryId);
			
			session.save(efDictionary);
			
			
			
		} catch (RuntimeException re) {
			log.error("saveWithAutoGeneratorItemValue(EfDictionary efDictionary, String groupName) failed", re);
			throw re;
		}
	}
	
	
	/**
	 *  可供转入的餐台
	 * @return
	 */
	public List<EfDictionary> getCateringForChange(){
		log.debug("finding  EfDictionary instances");
		try {
			StringBuffer queryString = new StringBuffer("from EfDictionary a where 1=1 ");
			
				queryString.append(" and a.id.groupname = 'DicCurrentState' ");
			
				queryString.append(" and a.id.itemvalue not in (2,4) ");
			
			queryString.append(" order by a.type desc,a.id.groupname,a.id.itemvalue ");
			
			return getHibernateTemplate().find(queryString.toString());
		} catch (RuntimeException re) {
			log.error("find  failed", re);
			throw re;
		}
	}
}